/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-19
 * Time:10:56
 */

import java.util.PriorityQueue;
import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * 定时器的实现
 */

//这类表示一个任务
//描述任务
class Mytask implements Comparable<Mytask>{
    //要执行的任务,runnable表示任务
    private Runnable runnable;
    //执行任务的时间(时间戳)
    private long time;

    public Mytask(Runnable runnable,long delay){
        this.runnable = runnable;
        //从现在开始计时过delay时间
        this.time = System.currentTimeMillis() + delay;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }
    //比较器
    @Override
    public int compareTo(Mytask o) {
        return (int) (this.time - o.time);
    }
}

class MyTimer{
    //利用优先级队列,短的时间先入,但是PriorityQueue线程不安全,所以可以使用阻塞队列
    private BlockingQueue<Mytask> queue = new PriorityBlockingQueue<>();

    private Object locker = new Object();
    public MyTimer(){
        //创建一个扫描线程
        Thread t = new Thread(() -> {
            while (true) {
                try {
                    synchronized (locker) {
                        //取出队首元素

                        Mytask task = queue.take();
                        long curTime = System.currentTimeMillis();
                        if (curTime >= task.getTime()) {
                            //到点了,要执行任务了
                            task.getRunnable().run();
                        } else {
                            //还没到点
                            //阻塞队列无法阻塞到取出另一个队首元素,需要再塞回去
                            queue.put(task);
                            // 没到点, 就等待
                            locker.wait(task.getTime() - curTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

    public void schedule(Runnable runnable,long after) throws InterruptedException {

            Mytask mytask = new Mytask(runnable, after);
            //任务添加到阻塞队列当中,后面需要扫描线程来扫描首元素
            queue.put(mytask);
        synchronized (locker) {
            //唤醒wait()
            locker.notify();
        }

    }
}


public class DemoTimer {
    public static void main(String[] args) throws InterruptedException {
        MyTimer timer = new MyTimer();
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到1!");
            }
        }, 3000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到2!");
            }
        }, 4000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到3!");
            }
        }, 5000);
        System.out.println("开始计时");
    }
}
